package b1w1nn1ng.crossf1t.business;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import b1w1nn1ng.crossf1t.db.Database;

public class Tabata extends Workout {
		
		int score;
		
		public Tabata(int userID, boolean prescribed, Calendar date) throws SQLException, ClassNotFoundException {
			this.userID = userID;
			this.type = "tabata";
			exercises = new ArrayList<Exercise>();
			this.prescribed = prescribed;
			this.cal = date;
			Workout.incrementWorkoutCount(userID);
		}
		
		public Tabata(int userID, ArrayList<Exercise> exercises, boolean prescribed,  Calendar cal, int score){
			this.userID = userID;
			this.exercises = exercises; 
			this.type = "Tabata";
			this.prescribed = prescribed;
			this.cal = cal;
			this.score = score;
		}

		public String toHTML() {
			StringBuilder ret = new StringBuilder();
			ret.append("Workout type: "+type+"<br />");
			ret.append("Date: "+CalendarDisplay.dateToString(cal)+"<br />");
			ret.append("Score: "+score+"<br />");
			ret.append("<br />Exercises: <br />");
				for(Exercise e : exercises) 
					ret.append(e.toHTML()+"<br />");
			ret.append(prescribed?"Performed as prescribed\n":"Not performed as prescribed<br />");
			return ret.toString();
		}
		
		@Override
		public boolean isPr(int id, String type, Exercise[] exercises, int value) throws Exception {
			Workout w = getPR(id, type,exercises);
			if(! (w instanceof Tabata))
				throw new Exception("getPR did not return an Tabata object");

			return ((Tabata)w).score == value;
		}

		public int getScore() {
			return score;
		}

		public void setScore(int score) {
			this.score = score;
		}

		@Override
		public int compareTo(Workout o) {
			return this.score-((Tabata)o).getScore();
		}
		public boolean getPrescribed(){
			return this.prescribed;
		}
		
		public Calendar getDate(){
			return this.cal;
		}
		
		public ArrayList<Exercise> getExercises(){
			return this.exercises;
		}
		
		@Override
		public void writeToDatabase() {
			try {
				Database.addWorkout(this);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		 
}